package MathTextModeller
{
	import MathText.WordProblem;
	
	import MyDisplayObjects.TabButton;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/** This is a container class for the tools.  Provides a space for a ToolFace object.  Tabs across top allow switching between ToolFace objects. 
	 */
	public class ToolDisplay extends MovieClip
	{
		// Variables acting like constants
		public static var BORDER:Number;
		// private vars
		protected var tabs:Array;
		protected var faces:Array;
		protected var currentTab:TabButton = null;
		protected var currentFace:ToolFace = null;
		protected var toolOptionsXML:XML, contextToolsXML:XML;
		protected var wordProblem:WordProblem;
		
		public function get currentFaceType():String{if (currentFace != null){return currentFace.type;} else {return "";}}
		
		public function ToolDisplay(width:Number, height:Number, tabNames:Array, toolOptionsXML:XML, contextToolsXML:XML, wordProblem:WordProblem, initialToolDisplay:String="")
		{
			var tab:TabButton;
			var face:ToolFace;
			BORDER = width/50;
			this.graphics.drawRect(0, 0, width, height);
			this.contextToolsXML = contextToolsXML;
			this.wordProblem = wordProblem;
			// determine what should be the first tool set to show
			if (toolOptionsXML.(attribute("initialDisplay") == "previous").length() > 0 && initialToolDisplay != "")
			{
				initialToolDisplay = initialToolDisplay;
			} else if (toolOptionsXML.(attribute("initialDisplay") == "congruent").length() > 0)
			{
				initialToolDisplay = wordProblem.type;
			} else if (toolOptionsXML.(attribute("initialDisplay") == "random").length() > 0)
			{
				var arr:Array = new Array("combine", "change", "compare");
				initialToolDisplay = arr[Math.floor(Math.random()*3)];
			} else if (toolOptionsXML.(attribute("initialDisplay") == "combine").length() > 0)
			{
				initialToolDisplay = "combine";
			} else if (toolOptionsXML.(attribute("initialDisplay") == "change").length() > 0)
			{
				initialToolDisplay = "change";
			} else if (toolOptionsXML.(attribute("initialDisplay") == "compare").length() > 0)
			{
				initialToolDisplay = "compare";
			} 
			
			
			tabs = new Array();
			faces = new Array();
			for each (var tabName:String in tabNames)
			{
				var allowInteraction:Boolean = true, doNotShow:Boolean = false;
				// are we restricting this type of problem
				if (toolOptionsXML.(attribute("allowSwitching") == "false").length() > 0 && tabName.toLowerCase() != wordProblem.type)
				{
					allowInteraction = false;
					if (toolOptionsXML.(attribute("displayUnusableTools") == "false").length() > 0)
					{
						doNotShow = true;
					}
				}
				if (!doNotShow)
				{
					// create the appropriate face
					if (tabName.toLowerCase() == "combine")
					{
						tab = new TabButton (width/3, width/6, "North", "roundRect", width/6, tabName.toLowerCase(), CombineFace.COLOR, "compare ", false);
						face = new CombineFace(this.width-2*BORDER, this.height-2*BORDER-tab.y-tab.height, contextToolsXML..combine[0], wordProblem);
					} else if (tabName.toLowerCase() == "change")
					{
						tab = new TabButton (width/3, width/6, "North", "roundRect", width/6, tabName.toLowerCase(), 0xFFFFEE, "compare ", false);
						face = new ChangeFace(this.width-2*BORDER, this.height-2*BORDER-tab.y-tab.height, contextToolsXML..change[0], wordProblem);
					} else if (tabName.toLowerCase() == "compare")
					{
						tab = new TabButton (width/3, width/6, "North", "roundRect", width/6, tabName.toLowerCase(), 0xEEFFEE, "compare ", false);
						face = new CompareFace(this.width-2*BORDER, this.height-2*BORDER-tab.y-tab.height, contextToolsXML..compare[0], wordProblem);
					}
					
					if (allowInteraction) tab.addEventListener(Event.COMPLETE, handleTabSelected);
					this.addChild(tab);
					tab.x = tabs.length * width/3;
					tabs.push(tab);
					
					// align tab
					face.x = BORDER;
					face.y = tab.y + tab.height + BORDER;
					faces.push(face);
				}
			}
			if (tabs.length > 1)
			{
				selectTab(tabNames.indexOf(initialToolDisplay));
			} else
			{
				selectTab(0);
			}
		}
		/** Handle when tab has been selected */
		private function handleTabSelected (event:Event):void
		{
			// add appropriate face by searching
			for (var index:uint = 0; index < tabs.length; index++)
			{
				if (event.currentTarget == tabs[index])
				{
					selectTab(index);	
					return;
				}
			}
		}
	
		/** Select a tab by the given index */
		public function selectTab(index:int):void
		{	
			if (index >= 0 && index < tabs.length)
			{
				// deselect current
				if (currentTab != null)
				{
					currentTab.deselect();
					currentTab = null;
					if (currentFace != null && currentFace.parent == this)
					{
						this.removeChild(currentFace);	
					}
				}
				// select new	
				currentTab = tabs[index];	
				currentFace = faces[index];
				this.addChild(currentFace);		
			}
		}
		
		/** If tab is selected, deselect */
		public function enableButton(name:String):Boolean
		{
			return this.currentFace.enableButton(name);
		}
	}
}